package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.IOUtil;
import com.ruoyi.system.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CompanyCategroyMapper;
import com.ruoyi.system.service.ICompanyCategroyService;

/**
 * 商家商品分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-20
 */
@Service
public class CompanyCategroyServiceImpl implements ICompanyCategroyService 
{
    @Autowired
    private CompanyCategroyMapper companyCategroyMapper;

    /**
     * 查询分类数据，返回JSON格式
     *
     * @param companyCategroy
     * @return
     */
    @Override
    public AjaxResult getJsonData(CompanyCategroy companyCategroy) {
        JSONArray jsa = new JSONArray();
        List<CompanyCategroy> list = selectCompanyCategroyList(companyCategroy);
        //将所有数据转入JSA中
        if (list != null && list.size() > 0) {
            for (CompanyCategroy ccate : list) {
                JSONObject bcate_json = ccate.toJSONObject();
                jsa.add(bcate_json);
                //如果不是一级分类，则添加到父级的children中
                if(ccate.getLevel()>1){
                    for(int i=0;i<jsa.size();i++){
                        JSONObject jso = jsa.getJSONObject(i);
                        if(jso.get("id").equals(ccate.getFid())){
                            if(!jso.containsKey("children")){
                                jso.put("children", new JSONArray());
                            }
                            jso.getJSONArray("children").add(bcate_json);
                        }
                    }
                }
            }
        }

        //将所有1级数据装入JSONArray中
        JSONArray jsa1 = new JSONArray();
        for (int i = 0; i < jsa.size(); i++) {
            JSONObject jso = jsa.getJSONObject(i);
            if (jso.get("level").equals(1)) {
                jsa1.add(jso);
            }
        }
        //返回JSON格式数据
        AjaxResult ar = AjaxResult.success();
        ar.put("data", jsa1);
        return ar;

    }

    /**
     * 查询商家商品分类
     * 
     * @param id 商家商品分类主键
     * @return 商家商品分类
     */
    @Override
    public CompanyCategroy selectCompanyCategroyById(String id)
    {
        return companyCategroyMapper.selectCompanyCategroyById(id);
    }

    /**
     * 查询商家商品分类列表
     * 
     * @param companyCategroy 商家商品分类
     * @return 商家商品分类
     */
    @Override
    public List<CompanyCategroy> selectCompanyCategroyList(CompanyCategroy companyCategroy)
    {
        return companyCategroyMapper.selectCompanyCategroyList(companyCategroy);
    }

    /**
     * 新增商家商品分类
     * 
     * @param companyCategroy 商家商品分类
     * @return 结果
     */
    @Override
    public int insertCompanyCategroy(CompanyCategroy companyCategroy)
    {
        //处理ID
        if(companyCategroy.getId() == null || companyCategroy.getId().equals("")){
            companyCategroy.setId(IOUtil.getId(4));
        }
        //处理LEVEL
        if(companyCategroy.getLevel() == null){
            if(companyCategroy.getFid() == null){
                companyCategroy.setFid("0");
                companyCategroy.setLevel(1);
            }else{
                CompanyCategroy cCategroy = selectCompanyCategroyById(companyCategroy.getFid());
                companyCategroy.setLevel(cCategroy.getLevel()-0+1);
            }
        }

        return companyCategroyMapper.insertCompanyCategroy(companyCategroy);
    }

    /**
     * 修改商家商品分类
     * 
     * @param companyCategroy 商家商品分类
     * @return 结果
     */
    @Override
    public int updateCompanyCategroy(CompanyCategroy companyCategroy)
    {
        return companyCategroyMapper.updateCompanyCategroy(companyCategroy);
    }

    /**
     * 批量删除商家商品分类
     * 
     * @param ids 需要删除的商家商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCompanyCategroyByIds(String[] ids)
    {
        return companyCategroyMapper.deleteCompanyCategroyByIds(ids);
    }

    /**
     * 删除商家商品分类信息
     * 
     * @param id 商家商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCompanyCategroyById(String id)
    {
        return companyCategroyMapper.deleteCompanyCategroyById(id);
    }
}
